richvariables.js ➔ setWithExpiry   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
1
/**
2
 * Rich Variables plugin for Craft CMS
3
 *
4
 * Rich Variables JS
5
 *
6
 * @author    nystudio107
7
 * @copyright Copyright (c) 2017 nystudio107
8
 * @link      https://nystudio107.com
9
 * @package   RichVariables
10
 * @since     1.0.18
11
 */
12
13
(function($R)
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
14
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
15
    function setWithExpiry(key, value, ttl) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
16
        const now = new Date()
17
        // `item` is an object which contains the original value
18
        // as well as the time when it's supposed to expire
19
        const item = {
20
            value: value,
21
            expiry: now.getTime() + ttl,
22
        }
23
        localStorage.setItem(key, JSON.stringify(item))
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
    }
25
26
    function getWithExpiry(key) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
27
        const itemStr = localStorage.getItem(key)
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
28
        // if the item doesn't exist, return null
29
        if (!itemStr) {
30
            return null
31
        }
32
        const item = JSON.parse(itemStr)
33
        const now = new Date()
34
        // compare the expiry time of the item with the current time
35
        if (now.getTime() > item.expiry) {
36
            // If the item is expired, delete the item from storage
37
            // and return null
38
            localStorage.removeItem(key)
39
            return null
40
        }
41
        return item.value
42
    }
43
44
    $R.add('plugin', 'richvariables', {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
45
        translations: {
46
            en: {
47
                "variables": "Variables"
48
            }
49
        },
50
        init: function(app)
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
51
        {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
52
            this.app = app;
53
            this.lang = app.lang;
54
            this.inline = app.inline;
55
            this.toolbar = app.toolbar;
56
            this.insertion = app.insertion;
57
58
            // Try to grab the menu from our local storage cache if possible
59
            var cachedResponseVars = getWithExpiry('rich-variables-menu-cache');
60
            if (cachedResponseVars === null) {
61
                // Grab the globals set Reference Tags from our controller
62
                var request = new XMLHttpRequest();
63
                request.open('GET', Craft.getActionUrl('rich-variables'), false);
0 ignored issues
show
Bug introduced by
The variable Craft seems to be never declared. If this is a global, consider adding a /** global: Craft */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
64
                request.onload = function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
65
                    if (request.status >= 200 && request.status < 400) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
66
                    } else {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
67
                    }
68
                };
69
                request.send();
70
                this.request = request;
71
            }
72
        },
73
        start: function()
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
74
        {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
75
            var dropdown = {};
76
            // Try to grab the menu from our local storage cache if possible
77
            var responseVars = getWithExpiry('rich-variables-menu-cache');
78
            if (responseVars === null && this.request) {
79
                responseVars = JSON.parse(this.request.responseText);
80
                setWithExpiry('rich-variables-menu-cache', responseVars, 60 * 1000)
81
            }
82
            // Iterate through each menu item, adding them to our dropdown
83
            responseVars.variablesList.forEach(function(menuItem, index) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
84
                var key = 'point' + (index + 1);
85
                var refTag = '<ins>' + menuItem.text + '</ins>';
86
                dropdown[key] = {
87
                    title: menuItem.title,
88
                    api: 'plugin.richvariables.insert',
89
                    args: refTag
90
                };
91
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
92
            // Handle empty menu items
93
            if (responseVars.variablesList.length === 0) {
94
                dropdown.point1 = {
95
                    title: "No Globals Found",
96
                    func: function(buttonName) {
0 ignored issues
show
Unused Code introduced by
The parameter buttonName is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
97
                        // NOP
98
                    },
99
                };
100
            }
101
            // Add the button and dropdown
102
            var $button = this.toolbar.addButton('variables', { title: this.lang.get('variables') });
103
            $button.setDropdown(dropdown);
104
            if (responseVars.useIconForMenu) {
105
                $button.setIcon('<img src="' + responseVars.menuIconUrl + '" height="16" width="16" style="margin-top: -2px;">');
106
            }
107
        },
108
        insert: function(refTag)
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
109
        {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
110
            this.insertion.insertRaw(refTag);
111
        }
112
    });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
113
})(Redactor);
0 ignored issues
show
Bug introduced by
The variable Redactor seems to be never declared. If this is a global, consider adding a /** global: Redactor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
114